package com.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beans.Company;
import com.beans.Position;
import com.beans.User;
import com.beans.UserLog;
import com.dao.UserDao;

public class UserDaoImpl extends UniversalDaoImpl implements UserDao {

	public List getListUser(String rollerCode) {
		String sql;
		sql = "select roller_code,loginName,name,pwd,sex,email,region_treeCode,address,qq,interest,description,ip,loginTimes,enabled,registTime from user where roller_code='"
				+ rollerCode + "'";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public User login(String name, String pwd,String ip) {
		User u = null;
		String sql = "from com.beans.User where loginName=? and pwd=?";
		Object[] p = new Object[] { name, pwd };
		List l = this.getHibernateTemplate().find(sql, p);
		if (l != null && l.size() > 0) {
			u = (User) l.get(0);
			excuteSQL("insert j_user_log(userID,ip) values("+u.getId()+",'"+ip+"')");
		}
		return u;
	}

	public com.vo.Page list(int pageNo, int pageSize, User user) {
		String sql = "select id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation,"
				+ "dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode,"
				+ "dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,tel,phone,qq,email,address,summary,"
				+ "views,ip,loginTimes,enabled,registTime,fix from j_user where 1=1";
		Map param = new HashMap();
		if (user.getRealName() != null && !"".equals(user.getRealName()))
			param.put("realName like ?", "%" + user.getRealName() + "%");
		if (user.getSex() != null && !"".equals(user.getSex()))
			param.put("sex=?", user.getSex());
		if (user.getWorkType() != null && !"".equals(user.getWorkType()))
			param.put("workType=?", user.getWorkType());
		if (user.getDegree() != null && !"".equals(user.getDegree()))
			param.put("degree like ?", user.getDegree() + "%");
		if (user.getWorkYear() > 0)
			param.put("workYear=?", user.getWorkYear());
		if (user.getMajor() != null && !"".equals(user.getMajor()))
			param.put("major like ?", user.getMajor() + "%");
		return Page(pageNo, pageSize, sql, "id", param);
	}

	public Map getUser(int id) {
		String sql = "select id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation,"
				+ "dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode,"
				+ "dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,tel,phone,qq,email,address,summary,"
				+ "views,ip,loginTimes,enabled,registTime,fix from j_user where id=?";
		String exSql="update j_user set views=views+1 where id="+id;
		this.excuteSQL(exSql);
		return getMap(sql, new Object[] { id });
	}

	public com.vo.Page myFocusCompany(int pageNo, int pageSize, int userid,
			Company c) {
		String sql = "select c.* from j_company c where c.id in(select distinct companyid from j_user_to_company_history where userid="
				+ userid + ")";
		Map param = new HashMap();
		if (c.getcName() != null && !"".equals(c.getcName()))
			param.put("cname like ?", "%" + c.getcName() + "%");
		return Page(pageNo, pageSize, sql, "cname", param);
	}

	public com.vo.Page myFocusPosition(int pageNo, int pageSize, int userid,
			Position p) {
		String sql = "select * from (select distinct p.id,p.companyID,c.cname company,dbo.getCodeName('zw',name) name,dbo.getCodeName('1',region) region,dbo.getCodeName('2',sex) sex,wages,dbo.getCodeName('5',workType) workType,persons,days,p.createtime,detail,orderid,p.views from j_position p left join j_company c on p.companyID=c.id,j_user_to_position_history h where h.positionid=p.id and h.userid="
				+ userid + " ) t";
		Map param = new HashMap();
		if (p.getDetail() != null && !"".equals(p.getDetail()))
			param.put("detail like ?", "%" + p.getDetail() + "%");
		if (p.getRegion() != null && !"".equals(p.getRegion()))
			param.put("region like ?", p.getRegion() + "%");
		if (p.getName() != null && !"".equals(p.getName()))
			param.put("name=?", p.getName());
		if (p.getCompany() != null)
			param.put("companyId=?", p.getCompany().getId());
		if (p.getId() > 0)
			param.put("p.id=?", p.getId());
		return Page(pageNo, pageSize, sql, "name", param);
	}

	public boolean myPwd(int userid, String oldPwd, String newPwd) {
		String sql = "update j_user set pwd=?,pwd1=pwd,pwd2=pwd1,pwd3=pwd2 where id=? and pwd=?";
		int t = getJdbcTemplate().update(sql,
				new Object[] { newPwd, userid, oldPwd });
		if (t == 1)
			return true;
		return false;
	}

	public List topFixUser(int top) {
		String sql = "select top "+top+" id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation,"
				+ "dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode,"
				+ "dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,"
				+ "tel,phone,qq,email,address,summary,views,ip,loginTimes,enabled,registTime from j_user where enabled='1' order by fix desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public List topNewUser(int top) {
		String sql = "select top "+top+" id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation,"
				+ "dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode,"
				+ "dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,"
				+ "tel,phone,qq,email,address,summary,views,ip,loginTimes,enabled,registTime from j_user where enabled='1' order by registTime desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public List topViewUser(int top) {
		String sql = "select top "+top+" id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation,"
				+ "dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode,"
				+ "dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,"
				+ "tel,phone,qq,email,address,summary,views,ip,loginTimes,enabled,registTime from j_user where enabled='1' order by views desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public void fix(int userID,int fix) {
		String sql="update j_user set fix="+fix+" where id="+userID;
		logger.info(sql);
		excuteSQL(sql);
	}

	public com.vo.Page getLog(int pageNo, int pageSize, UserLog log) {
		String sql = "select l.id,l.userID,l.ip,l.createTime,a.loginName,a.realName from j_user_log l,j_user a where a.id=l.userID";
		Map param=new HashMap();
		if(log.getUserID()>0)param.put("l.userID=?", log.getUserID());
		if(log.getLoginName()!=null&&!"".equals(log.getLoginName()))param.put("a.loginName like ?", "%"+log.getLoginName()+"%");
		return Page(pageNo, pageSize, sql,"createTime", param);
	}

}